package it.koutifaris.travianmap.view.mainview;

import static it.koutifaris.travianmap.communication.mapdownloader.DownloadProgressHelper.downloadServiceHelper;
import it.koutifaris.travianmap.R;
import it.koutifaris.travianmap.communication.mapdownloader.DownloadProgressHandler;
import it.koutifaris.travianmap.context.HasApplicationContex;
import it.koutifaris.travianmap.context.TravianMapContext;
import it.koutifaris.travianmap.dal.TravianServerService;
import it.koutifaris.travianmap.dal.TravianServerService.TravianServer;
import it.koutifaris.travianmap.view.commandlistview.ListElement;
import it.koutifaris.travianmap.view.mainview.ServerItemView.OnDownloadFinishedHandler;

import java.util.Date;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

class MainListAdapter extends ArrayAdapter<ListElement> {

	private final MapListView view;

	public MainListAdapter(TravianMapContext context, MapListView view) {
		super(context.getActivity(), android.R.layout.simple_list_item_1);
		this.view = view;

		int numServers = queryForTravianServers(context);
		if (numServers <= 0 || true) {
			String demoMapLabel = context.getActivity().getResources().getString(R.string.maplist_demo_map);
			this.add(new ListElement(demoMapLabel, new LoadRandomMapCommand()));
		}
	}

	private int queryForTravianServers(TravianMapContext context) {
		TravianServerService service = context.getServerServiceFactory().travianServerService();

		List<TravianServer> listServers = service.listServers();
		for (TravianServer travianServer : listServers) {
			this.add(new ServerListElement(travianServer));
		}

		service.close();

		return listServers.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View androidView;

		ListElement item = this.getItem(position);

		if (item instanceof ServerListElement) {
			ServerListElement serverListElement = (ServerListElement) item;
			if (convertView != null) {
				androidView = convertView;
			} else {
				androidView = view.newItemView(parent);
			}

			TravianServer server = serverListElement.getServer();
			downloadServiceHelper().updateProgressHandlerIfNeeded(server.getServerId(), androidView);
			ServerItemView viewItem = view.viewItemFor(androidView);

			DownloadProgressHandler downloadProgressHandler =
					viewItem.getDownloadProgressHandler(new OnFinishHandlerImpl(viewItem, server.getServerId()));

			viewItem
					.setLabel(server.getServerId())
					.setlastUpdate(server.getLastUpdate())
					.setIconClickHandler(new DownloadOnClickListener(server, downloadProgressHandler));

		} else {
			androidView = view.newItemView(parent);
			view.viewItemFor(androidView)
					.setLabel(item.getLabel())
					.setlastUpdate(new Date());
		}

		return androidView;
	}

	private static class OnFinishHandlerImpl extends HasApplicationContex implements OnDownloadFinishedHandler {

		private final ServerItemView viewItem;
		private final String serverId;

		public OnFinishHandlerImpl(ServerItemView viewItem, String serverId) {
			this.viewItem = viewItem;
			this.serverId = serverId;
		}

		@Override
		public void onDownloadFinished() {
			TravianServerService travianServerService = getTravianMapContext().getServerServiceFactory().travianServerService();
			TravianServer server = travianServerService.getServer(serverId);

			viewItem.setlastUpdate(server.getLastUpdate());

			travianServerService.close();
		}
	}

}
